package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.policeasset.mapper.ProjectDesignMapper;
import com.zygh.policeasset.mapper.SectionMapper;
import com.zygh.policeasset.mapper.UploadFileMapper;
import com.zygh.policeasset.pojo.ProjectDesign;
import com.zygh.policeasset.pojo.TableType;
import com.zygh.policeasset.pojo.UploadFile;
import com.zygh.policeasset.pojo.vo.ProjectDesignVO;
import com.zygh.policeasset.utils.AuditUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ProjectDesignService {
    @Autowired
    ProjectDesignMapper projectDesignMapper;

    @Autowired
    AuditFlowService auditFlowService;

    @Autowired
    UploadFileMapper uploadFileMapper;

    @Autowired
    SectionMapper sectionMapper;

    public PageInfo findAll(Integer pageNum, Integer pageSize, Integer status,
                            String projectNo, String time1, String time2, Integer userId) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProjectDesignVO> projectDesignList = projectDesignMapper.findAll(status, projectNo, time1, time2, userId);
        for (ProjectDesignVO projectDesignVO : projectDesignList) {
            if (!StringUtils.isEmpty(projectDesignVO.getSectionIds())) {
                List<String> stringList = sectionMapper.findByIds(projectDesignVO.getSectionIds().split(","));
                projectDesignVO.setSectionNames(stringList);
            }
        }

        return new PageInfo<>(projectDesignList);
    }

    @Transactional(rollbackFor = {Exception.class})
    public Integer add(ProjectDesign projectDesign) {
        try {
            String now = DateUtil.now();
            String flowNo = AuditUtil.getFlowNo(now);
            projectDesign.setFlowNo(flowNo);

            Integer count = projectDesignMapper.add(projectDesign);

            auditFlowService.add(projectDesign.getUserId(), flowNo, now, TableType.PROJECTDESIGN.getName(),
                    TableType.PROJECTDESIGN.getType(), projectDesign.getProjectNo(), null);

            List<UploadFile> uploadFileList = projectDesign.getUploadFileList();

            if (uploadFileList.size() > 0) {
                for (int i = 0; i < uploadFileList.size(); i++) {
                    uploadFileList.get(i).setProjectId(projectDesign.getId());
                    uploadFileList.get(i).setProjectName("project_design");
                    uploadFileList.get(i).setCreateDate(DateUtil.now());
                    this.uploadFileMapper.add(uploadFileList.get(i));
                }
            }
            return count;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return 0;
        }
    }

    public Integer update(ProjectDesign projectDesign) {
        return projectDesignMapper.update(projectDesign);
    }

    public Integer delete(Integer id) {
        return projectDesignMapper.delete(id);
    }

    public ProjectDesignVO findById(Integer id) {
        ProjectDesignVO projectDesignVO = projectDesignMapper.findById(id);
        if (!StringUtils.isEmpty(projectDesignVO.getSectionIds())) {
            List<String> stringList = sectionMapper.findByIds(projectDesignVO.getSectionIds().split(","));
            projectDesignVO.setSectionNames(stringList);
        }
        return projectDesignVO;
    }
}